#!/bin/bash
set -e -o pipefail

# NOW_UNIX_TIME=`date +%s`000
if [ -d "${SS_PATH}" ]; then 

  PROC_PATH="/opt/fonsview/NE/ss/data/proc"
  if [ -d "$PROC_PATH" ];then
    NodeID=`fgrep "*" ${PROC_PATH}/mng/nodes | fgrep -v "*:"|awk -F "*| " '{print $2}'`
    PEMXLOAD=`fgrep ${NodeID} ${PROC_PATH}/mng/nodes |awk '{print $5}'`
    PNSLOAD=`fgrep ${NodeID} ${PROC_PATH}/mng/nodes |awk '{print $6}'`
    PEDLOAD=`fgrep ${NodeID} ${PROC_PATH}/mng/nodes |awk '{print $7}'`
    HLSLOAD=`fgrep ${NodeID} ${PROC_PATH}/mng/nodes |awk '{print $8}'`
    HEMLOAD=`fgrep ${NodeID} ${PROC_PATH}/mng/nodes |awk '{print $9}'`
    STORAGE=`fgrep ${NodeID} ${PROC_PATH}/mng/nodes |awk '{print $10}'`

    PEMXLOAD_current=`printf  ${PEMXLOAD} |awk -F '/' '{print $1}'`
    PNSLOAD_current=`printf  ${PNSLOAD} |awk -F '/' '{print $1}'`
    PEDLOAD_current=`printf  ${PEDLOAD} |awk -F '/' '{print $1}'`
    HLSLOAD_current=`printf  ${HLSLOAD} |awk -F '/' '{print $1}'`
    HEMLOAD_current=`printf  ${HEMLOAD} |awk -F '/' '{print $1}'`
    STORAGE_current=`printf  ${STORAGE} |awk -F '/|GB' '{print $1}'`

    PEMXLOAD_max=`printf  ${PEMXLOAD} |awk -F '/' '{print $2}'`
    PNSLOAD_max=`printf  ${PNSLOAD} |awk -F '/' '{print $2}'`
    PEDLOAD_max=`printf  ${PEDLOAD} |awk -F '/' '{print $2}'`
    HLSLOAD_max=`printf  ${HLSLOAD} |awk -F '/' '{print $2}'`
    HEMLOAD_max=`printf  ${HEMLOAD} |awk -F '/' '{print $2}'`
    STORAGE_max=`printf  ${STORAGE} |awk -F '/|GB' '{print $3}'`


    echo "# HELP node_ss_mng_nodes exporter ss"
    echo "# TYPE node_ss_mng_nodes gauge"
    echo "node_ss_mng_nodes{current=\"PEMXLOAD\"} ${PEMXLOAD_current}"
    echo "node_ss_mng_nodes{current=\"PNSLOAD\"} ${PNSLOAD_current}"
    echo "node_ss_mng_nodes{current=\"PEDLOAD\"} ${PEDLOAD_current}"
    echo "node_ss_mng_nodes{current=\"HLSLOAD\"} ${HLSLOAD_current}"
    echo "node_ss_mng_nodes{current=\"HEMLOAD\"} ${HEMLOAD_current}"
    echo "node_ss_mng_nodes{current=\"STORAGE\"} ${STORAGE_current}"
    echo "node_ss_mng_nodes{current=\"PEMXLOAD_total\"} ${PEMXLOAD_max}"
    echo "node_ss_mng_nodes{current=\"PNSLOAD_total\"} ${PNSLOAD_max}"
    echo "node_ss_mng_nodes{current=\"PEDLOAD_total\"} ${PEDLOAD_max}"
    echo "node_ss_mng_nodes{current=\"HLSLOAD_total\"} ${HLSLOAD_max}"
    echo "node_ss_mng_nodes{current=\"HEMLOAD_total\"} ${HEMLOAD_max}"
    echo "node_ss_mng_nodes{current=\"STORAGE_total\"} ${STORAGE_max}"

  fi
fi
